﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HP.Cachalote.DataModels;
using HP.Cachalote.Services.Interfaces;
using HP.Cachalote.Services.Validations;

namespace HP.Cachalote.Services.Validations
{
    public class TodoItemValidator : DataValidatorBase<TodoItem>
    {
        public const string TitleErrorKey = "TodoItemTitle";
        public const string NoteErrorKey = "TodoItemNote";

        /// <summary>
        /// Validate the provided entity. Then check the IsValid property to see the result.
        /// </summary>
        /// <param name="entity">The provided entity to be validated.</param>
        public override void Validate(TodoItem entity)
        {
            CheckRequiredTitle(entity);
            CheckTitleLength(entity);
            CheckNoteLength(entity);
        }

        /// <summary>
        /// Check if the required title is provided.
        /// </summary>
        /// <param name="entity">The instance of TodoItem.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckRequiredTitle(TodoItem entity)
        {
            entity.Title = entity.Title.Trim();

            if (!ValidationHelper.RuleCheckRequiredContent(entity.Title))
            {
                AddError(TitleErrorKey, "Todo title is required.");
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the title's max length.
        /// </summary>
        /// <param name="entity">The instance of TodoItem.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckTitleLength(TodoItem entity)
        {
            int maxLength = 150;
            entity.Title = entity.Title.Trim();

            if (!ValidationHelper.RuleCheckMaxLength(maxLength, entity.Title))
            {
                AddError(TitleErrorKey, string.Format("The length of todo title exceed {0}.", maxLength));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the note's max length.
        /// </summary>
        /// <param name="entity">The instance of TodoItem.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckNoteLength(TodoItem entity)
        {
            int maxLength = 500;
            entity.Note = string.IsNullOrEmpty(entity.Note) ? string.Empty : entity.Note.Trim();

            if (!ValidationHelper.RuleCheckMaxLength(maxLength, entity.Note))
            {
                AddError(NoteErrorKey, string.Format("The length of todo note exceed {0}.", maxLength));
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
